home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 August (Alt) / CHIP 2005-08.1.iso / program / guvenlik / syslinux-3.07.exe / libfat / cache.c next >
Encoding:
C/C++ Source or Header  |  2004-12-15  |  1.6 KB  |  71 lines

  1. #ident "$Id: cache.c,v 1.1 2004/12/15 10:14:39 hpa Exp $"
  2. /* ----------------------------------------------------------------------- *
  3.  *   
  4.  *   Copyright 2004 H. Peter Anvin - All Rights Reserved
  5.  *
  6.  *   This program is free software; you can redistribute it and/or modify
  7.  *   it under the terms of the GNU General Public License as published by
  8.  *   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
  9.  *   Boston MA 02111-1307, USA; either version 2 of the License, or
  10.  *   (at your option) any later version; incorporated herein by reference.
  11.  *
  12.  * ----------------------------------------------------------------------- */
  13.  
  14. /*
  15.  * cache.c
  16.  *
  17.  * Simple sector cache
  18.  */
  19.  
  20. #include <stdlib.h>
  21. #include "libfatint.h"
  22.  
  23. void * libfat_get_sector(struct libfat_filesystem *fs, libfat_sector_t n)
  24. {
  25.   struct libfat_sector *ls;
  26.  
  27.   for ( ls = fs->sectors ; ls ; ls = ls->next ) {
  28.     if ( ls->n == n )
  29.       return ls->data;        /* Found in cache */
  30.   }
  31.  
  32.   /* Not found in cache */
  33.   ls = malloc(sizeof(struct libfat_sector));
  34.   if ( !ls ) {
  35.     libfat_flush(fs);
  36.     ls = malloc(sizeof(struct libfat_sector));
  37.   
  38.     if ( !ls )
  39.       return NULL;        /* Can't allocate memory */
  40.   }
  41.  
  42.   if ( fs->read(fs->readptr, ls->data, LIBFAT_SECTOR_SIZE, n) 
  43.        != LIBFAT_SECTOR_SIZE ) {
  44.     free(ls);
  45.     return NULL;        /* I/O error */
  46.   }
  47.  
  48.   ls->n = n;
  49.   ls->next = fs->sectors;
  50.   fs->sectors = ls;
  51.  
  52.   return ls->data;
  53. }
  54.  
  55. void libfat_flush(struct libfat_filesystem *fs)
  56. {
  57.   struct libfat_sector *ls, *lsnext;
  58.  
  59.   lsnext = fs->sectors;
  60.   fs->sectors = NULL;
  61.  
  62.   for ( ls = lsnext ; ls ; ls = lsnext ) {
  63.     lsnext = ls->next;
  64.     free(ls);
  65.   }
  66. }
  67.  
  68.  
  69.     
  70.   
  71.